<?php
    $this->layout('base.phtml', [
        'title' => _('Importation from an OPML file'),
        'canonical' => url_full('opml'),
        'has_errors' => $error || $errors,
        'back_options' => [
            'track' => false,
        ],
    ]);
?>

<div class="section section--small section--longbottom">
    <div class="section__title">
        <h1><?= _('Importation from an <abbr>OPML</abbr> file') ?></h1>
    </div>

    <p class="section__intro">
        <?= _('<abbr>OPML</abbr> enables you to import your feeds from another software.') ?>
    </p>

    <?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

    <?php if ($importation && $importation->status === 'ongoing'): ?>
        <p class="paragraph--featured" role="alert">
            <?= _('We’re importing your data…') ?>
        </p>

        <div class="spinner"></div>

        <p class="paragraph--placeholder">
            <?= _('This might take several minutes, please wait.') ?>
        </p>

        <form
             method="get"
             action="<?= url('opml') ?>"
             data-controller="autosubmit"
             data-autosubmit-mode-value="timeout"
             data-autosubmit-timeout-value="4000"
        >
        </form>
    <?php elseif ($importation && $importation->status === 'finished'): ?>
        <?= $this->include('alerts/_success.phtml', [
            'message' => _('We’ve imported your data from your <abbr>OPML</abbr> file. We’re now going to fetch their links in background.')
        ]) ?>

        <form method="post" action="<?= url('delete importation', ['id' => $importation->id]) ?>">
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
            <input type="hidden" name="from" value="<?= url('opml') ?>" />

            <div class="form__actions">
                <button type="submit" class="button--primary">
                    <?= _('Ok!') ?>
                </button>
            </div>
        </form>
    <?php elseif ($importation && $importation->status === 'error'): ?>
        <?= $this->include('alerts/_error.phtml', ['message' => protect($importation->error)]) ?>

        <form method="post" action="<?= url('delete importation', ['id' => $importation->id]) ?>">
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />
            <input type="hidden" name="from" value="<?= url('opml') ?>" />

            <div class="form__actions">
                <button type="submit" class="button--primary">
                    <?= _('Ok!') ?>
                </button>
            </div>
        </form>
    <?php else: ?>
        <form
            method="post"
            action="<?= url('import opml') ?>"
            data-controller="autosubmit form-file"
            enctype="multipart/form-data"
        >
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />

            <input
                type="file"
                name="opml"
                id="opml"
                accept=".opml, .xml"
                data-action="autosubmit#submit"
                data-form-file-target="file"
                hidden
            />

            <div class="form__actions">
                <button
                    type="button"
                    class="button--primary"
                    data-action="form-file#openFile"
                    aria-describedby="opml-desc"
                    data-autosubmit-target="actionButton"
                >
                    <?= _('Import a file') ?>
                </button>
            </div>

            <div id="opml-desc">
                <?php if (isset($errors['opml'])): ?>
                    <p class="form-group__error">
                        <span class="sr-only"><?= _('Error') ?></span>
                        <?= $errors['opml'] ?>
                    </p>
                <?php endif; ?>
            </div>
        </form>
    <?php endif; ?>
</div>
